package com.yueya.basic.feedback.service;

import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.feedback.model.Feedback;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

/**
 * 
 * 
 * @author yangfeng
 * 
 */
public class FeedbackService {

	public Page<Record> pagination(int pageNumber, int pageSize,
			Map<String, Object> map) {

		String select = "select a.*,(select u.user_name from t_user u where u.user_id = a.user_id) as user_name ";
		StringBuffer sb = new StringBuffer(
				" from t_feedback a  where a.del_flag = '0' ");

		if (map.get("status") != null && !"".equals(map.get("status"))) {
			sb.append(" and  a.status = '" + map.get("status") + "'  ");
		}

		sb.append("  order by a.create_time desc ");
		String sql = sb.toString();
		return Db.paginate(pageNumber, pageSize, select, sql);

	}

	@Before(Tx.class)
	public boolean save(Feedback feedback) {
		boolean bl = false;
		if (feedback != null && feedback.getStr("id") != null
				&& !"".equals(feedback.getStr("id"))) {
			feedback.set("update_time", DateUtil.getDate(DateUtil.timeFormat));
			bl = feedback.update();
		} else {
			feedback.set("id", UUIDGenerator.getUUID());
			feedback.set("create_time", DateUtil.getDate(DateUtil.timeFormat));
			bl = feedback.save();
		}
		return bl;
	}

}
